<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>添加或者修改课程类别</title>
	<link rel="stylesheet" href="/layui/css/layui.css"></link>
	<style type="text/css">

		table tr td{
			padding: 10px;
		}

	</style>
</head>
<body>
<div style="padding: 20px">
	<form method="post" >
		<table>
			<tr>
				<td>课程类别名称：</td>
				<td><input type="text" id="name" name="name" class="layui-input" style="width: 300px"/></td>
			</tr>
			<tr>
				<td>排列序号：</td>
				<td><input type="text" id="sort" name="sort" class="layui-input" style="width: 100px;display: inline;"/>&nbsp;&nbsp;<span>(根据数值从小到大排序)</span></td>
			</tr>
			<tr>
				<td>友情链接描述：</td>
				<td>
					<textarea type="text" id="remark" name="remark" class="layui-input" style="width: 300px;height: 100px" ></textarea>
				</td>
			</tr>
			<tr>
				<td><button class="layui-btn" onclick="submitData();return false;">提交</button></td>
				<td><font id="errorInfo" color="red"></font></td>
			</tr>
		</table>
	</form>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/common.js"></script>
<script type="text/javascript">

    layui.use(['form'], function(){

        var form = layui.form;


    });

    function submitData(){
        var name=$("#name").val().trim();
        var remark=$("#remark").val().trim();
        var sort=$("#sort").val().trim();
        if(name=="") {
            $("#errorInfo").text("请输入课程类别名称！");
            $("#name").focus();
            return false;
        }
        if(sort=="") {
            $("#errorInfo").text("请输入排列序号！");
            $("#sort").focus();
            return false;
        }
        if (!(/(^[1-9]\d*$)/.test(sort))) {
            $("#errorInfo").text("排列序号必须是正整数！");
            $("#sort").focus();
            return false;
        }
        var id=getQueryVariable("id");
        if(id){
            $.post("/admin/courseType/save",{id:id,name:name,sort:sort,remark:remark},function(result){
                if(result.success){
                    layer.alert('修改成功！',function () {
                        parent.reloadPage();
                    });
                }else{
                    $("#errorInfo").text(result.errorInfo);
                }
            },"json");
        }else{
            $.post("/admin/courseType/save",{name:name,sort:sort,remark:remark},function(result){
                if(result.success){
                    layer.alert('添加成功！',function () {
                        parent.reloadPage();
                    });
                }else{
                    $("#errorInfo").text(result.errorInfo);
                }
            },"json");
        }
    }



    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    $(function(){

        var id=getQueryVariable("id");

        if(id){
            $.post("/admin/courseType/findById",{id:id},function(result){
                if(result.success){
                    var courseType=result.courseType;
                    $("#name").val(courseType.name);
                    $("#remark").val(courseType.remark);
                    $("#sort").val(courseType.sort);
                }else{
                    layer.alert('服务器加载有问题，请联系管理员！');
                }
            },"json");
        }
    });


</script>
</body>
</html>